﻿using BigEgg.Framework.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;

namespace FMStudio.Documents.ProjectDocument.Test
{
    [TestClass]
    public class ProjectDocumentTest
    {
        private string TestPath1 = Path.Combine(Environment.CurrentDirectory, "Test1.fmproj");
        private string TestPath2 = Path.Combine(Environment.CurrentDirectory, "Test2.fmproj");


        [TestInitialize]
        public void TestCleanup()
        {
            if (File.Exists(TestPath1))
                File.Delete(TestPath1);
            if (File.Exists(TestPath2))
                File.Delete(TestPath2);
        }


        [TestMethod]
        public void DocumentModifyTest()
        {
            ProjectDocumentType documentType = new ProjectDocumentType();
            ProjectDocument document = documentType.New(TestPath1);
            documentType.Save(document);

            Assert.IsFalse(document.IsModified);
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified, () => document.Properties.Name = "NewName");
            Assert.IsTrue(document.IsModified);

            document.IsModified = false;
            AssertHelper.PropertyChangedEvent(document, x => x.IsModified, () => document.Properties.Description = "NewDescription");
            Assert.IsTrue(document.IsModified);

            document.IsModified = false;
            AssertHelper.PropertyChangedEvent(
                document, x => x.IsModified,
                () => document.Items.Add(
                    new ProjectItem()
                    {
                        Name = "NewItem"
                    }));
            Assert.IsTrue(document.IsModified);
        }

        [TestMethod]
        public void PropertiesWithNotification()
        {
            ProjectDocumentType documentType = new ProjectDocumentType();
            ProjectDocument document = documentType.New(TestPath1);

            AssertHelper.PropertyChangedEvent(document, x => x.Properties, () => document.Properties.Name = "NewName");
            Assert.AreEqual("NewName", document.Properties.Name);

            AssertHelper.PropertyChangedEvent(document, x => x.Properties, () => document.Properties.Description = "NewDescription");
            Assert.AreEqual("NewName", document.Properties.Name);
        }

        [TestMethod]
        public void AddAndRemoveItemTest()
        {
            ProjectDocumentType documentType = new ProjectDocumentType();
            ProjectDocument document = documentType.New(TestPath1);

            //  Add a item
            Assert.AreEqual(0, document.Items.Count);
            document.Items.Add(new ProjectItem()
            {
                Name = "NewItem"
            });
            Assert.AreEqual(1, document.Items.Count);

            //  Remove a item
            document.Items.Remove(document.Items.First());
            Assert.AreEqual(0, document.Items.Count);
        }

        [TestMethod]
        public void ModifyModelTest()
        {
            ProjectDocumentType documentType = new ProjectDocumentType();
            ProjectDocument document = documentType.New(TestPath1);

            document.Items.Add(new ProjectItem()
            {
                Name = "OldItem"
            });

            ProjectItem item = document.Items.Where(x => x.Name == "OldItem").First();
            Assert.IsNotNull(item);
            item.Name = "NewItem";
            Assert.AreEqual(1, document.Items.Count(x => x.Name == "NewItem"));
        }
    }
}
